package com.itheima.project;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.itheima.project.knowledge.*;
import com.itheima.project.mainPage.*;
import com.itheima.project.project.*;
import com.itheima.project.publicnumber.*;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements  FragmentMainPage.ScrollCallBack {

    //存储Fragment的容器
    private List<Fragment> mList = new ArrayList<>();
    //显示内容
    private ViewPager vpContent;
    //头部标题
    private TextView tvTitle;
    //底部菜单栏
    private RadioGroup rgBottom;
    //首页按钮
    private RadioButton rbMainPage;
    //知识体系按钮
    private RadioButton rbKnowledge;
    //公众号按钮
    private RadioButton rbPublic;
    //项目按钮
    private RadioButton rbProject;
    //我的按钮
    private Button btnMine;
    //首页 fragment
    private FragmentMainPage fragment_mainPage;
    //左侧 我的 页面
    private DrawerLayout dlLeft;
    private FragmentKnowledge fragment_knowledge;
    private RelativeLayout rlTop;
    private int MAX_ALPHA = 255;
    private ImageView ivTopping;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        dlLeft = findViewById(R.id.dl_left_main);
        tvTitle = findViewById(R.id.tv_title_activity_main);
        vpContent = findViewById(R.id.vp_content_activity);
        rgBottom = findViewById(R.id.rg_bottom_activity_main);
        rbMainPage = findViewById(R.id.rb_main_page_activity_main);
        rbKnowledge = findViewById(R.id.rb_knowledge_activity_main);
        rbPublic = findViewById(R.id.rb_public_activity_main);
        rbProject = findViewById(R.id.rb_project_activity_main);
        btnMine = findViewById(R.id.btn_mine_activity_main);
        rlTop = findViewById(R.id.rl_top_activity_main);
        ivTopping = findViewById(R.id.iv_topping_activity_main);
    }

    private void initData() {
        fragment_knowledge = new FragmentKnowledge();
        fragment_mainPage = new FragmentMainPage();
        mList.add(fragment_mainPage);
        mList.add(fragment_knowledge);
        mList.add(new FragmentPublic());
        mList.add(new FragmentProject());

        vpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));

        //设置 RadioButton的drawableTop资源大小调整
        RadioButton[] radioButtons = new RadioButton[4];
        radioButtons[0] = rbMainPage;
        radioButtons[1] = rbKnowledge;
        radioButtons[2] = rbPublic;
        radioButtons[3] = rbProject;

        for (RadioButton rb : radioButtons) {
            //挨着给每个RadioButton加入drawable限制边距以控制显示大小
            Drawable[] drawables = rb.getCompoundDrawables();
            //获取drawables
            Rect r = new Rect(0, 0, drawables[1].getMinimumWidth() * 1 / 2, drawables[1].getMinimumHeight() * 1 / 2);
            //定义一个Rect边界
            drawables[1].setBounds(r);
            //给指定的radiobutton设置drawable边界
//            if (rb.getId() == R.id.rb_more) {
//                r = new Rect(0, 0, drawables[1].getMinimumWidth(), drawables[1].getMinimumHeight());
//                drawables[1].setBounds(r);
//            }
            //添加限制给控件
            rb.setCompoundDrawables(null, drawables[1], null, null);
        }
    }

    private void initEvent() {
        rgBottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_main_page_activity_main:
                        vpContent.setCurrentItem(0);
                        break;
                    case R.id.rb_knowledge_activity_main:
                        vpContent.setCurrentItem(1);
                        break;
                    case R.id.rb_public_activity_main:
                        vpContent.setCurrentItem(2);
                        break;
                    case R.id.rb_project_activity_main:
                        vpContent.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }
        });

        btnMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dlLeft.openDrawer(Gravity.LEFT);
            }
        });
        ivTopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tvTitle.getText() == "首页"){
                    fragment_mainPage.scrollToTop();
                }else if(tvTitle.getText() == "知识体系"){
                    fragment_knowledge.scrollToTop();

                }

            }
        });

    }

    public class  MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
            switch (position){
                case 0:
                    rbMainPage.setChecked(true);
                    tvTitle.setText("首页");
                    //为 首页 下滑 隐藏 设置监听
                    fragment_mainPage.setScrollCallBack(MainActivity.this);

                    break;
                case 1:
                    rbKnowledge.setChecked(true);
                    tvTitle.setText("知识体系");
                    break;
                case 2:
                    rbPublic.setChecked(true);
                    tvTitle.setText("公众号");
                    break;
                case 3:
                    rbProject.setChecked(true);
                    tvTitle.setText("项目");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        float diff = rlTop.getAlpha() * 255 - dy;
        float alpha = diff/MAX_ALPHA;
        if(alpha < 0){
            rlTop.setAlpha(0.0f);
            rlTop.setVisibility(View.GONE);
            rgBottom.setVisibility(View.GONE);
            ivTopping.setVisibility(View.GONE);
        }else if(alpha >= 1){
            rlTop.setAlpha(1.0f);
            rlTop.setVisibility(View.VISIBLE);
            rgBottom.setVisibility(View.VISIBLE);
            ivTopping.setVisibility(View.VISIBLE);
        }else {
            rlTop.setVisibility(View.VISIBLE);
            rgBottom.setVisibility(View.VISIBLE);
            ivTopping.setVisibility(View.VISIBLE);
            rlTop.setAlpha(alpha);
        }
    }
    @Override
    public void onLeaveScreen() {
        float alpha = rlTop.getAlpha();
        if(alpha > 0){
            rlTop.setAlpha(1.0f);
            rlTop.setVisibility(View.VISIBLE);
        }else {
            rlTop.setAlpha(0.0f);
            rlTop.setVisibility(View.GONE);
        }
    }

}
